package com.example.tutorial.protos;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        Person john = Person.newBuilder()
                .setName("John")
                .setId(111)
                .setEmail("email@email.com")
                .addPhones(Person.PhoneNumber.newBuilder()
                        .setNumber("12345566777")
                        .setType(Person.PhoneType.PHONE_TYPE_HOME)
                )
                .build();
        AddressBook addressBook = AddressBook.newBuilder().addPeople(john).addPeople(john).build();
        List<Person> peopleList = addressBook.getPeopleList();
        System.out.println(peopleList);
        System.out.println("++++++++++++++++++++++++");
        System.out.println(addressBook);

        try(FileOutputStream os = new FileOutputStream(System.getProperty("user.dir") + "\\test.txt");) {
            addressBook.writeTo(os);
            os.flush();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

        try(FileInputStream in = new FileInputStream(System.getProperty("user.dir") + "\\test.txt")) {
            AddressBook addressBook1 = AddressBook.parseFrom(in);
            System.out.println(addressBook1);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }



    }
}
